package com.cwj.lab3.博客需要.dfs.种子问题;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class floodFill {
    public floodFill(String path) {
        this.readGraph(path);
    }

    int[][]pic;
    final int[][]direction={{0,1},{1,0},{-1,0},{0,-1}};
    public  void readGraph(String path){
        try {
            Scanner in=new Scanner(new File(path));
            pic=new int[in.nextInt()][in.nextInt()];
            for (int i = 0; i < pic.length; i++) {
                for (int j = 0; j < pic[i].length; j++) {
                    pic[i][j]=in.nextInt();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public int fill(){
        int xLen=this.pic.length;
        int yLen=this.pic[0].length;
        boolean[][]vis=new boolean[xLen][yLen];
        int cnt=0;
        for (int i = 0; i < this.pic.length; i++) {
            for (int j = 0; j < this.pic[i].length; j++) {
                if(pic[i][j]==1&&!vis[i][j]){
                    cnt+=1;
                    dfs(i,j,vis);
                }
            }
        }
        return cnt;
    }
    private void dfs(int x,int y,boolean[][]vis){
        if(x<0||y<0||x>=vis.length||y>=vis[0].length){
            return;
        }
        if(vis[x][y]||pic[x][y]!=1){
            return ;
        }
        vis[x][y]=true;
//        for (int i = -1; i <= 1; i++) {
//            for (int j = -1; j <=1 ; j++) {
//                    dfs(x+i,y+j,vis);
//            }
//        }
        for (int i = 0; i < this.direction.length; i++) {
            dfs(x+this.direction[i][0],y+this.direction[i][1],vis);
        }
    }


    public static void main(String[] args) {
        floodFill f=new floodFill("src/src/com/cwj/lab3/博客需要/dfs/种子问题/种子问题测试集");
        System.out.println(f.fill());
    }
}
